<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<style type="text/css">
#edit_supplier_info .box p>label>span {
	display: inline-block;
	width: 120px;
	text-align: right;
}

#edit_supplier_info  .formBox {
	padding: 0;
}

#edit_supplier_info .formBox a {
	text-decoration: none;
	color: #2b7dbc;
}

#edit_supplier_info .formBox textarea {
	width: 640px;
	height: 100px;
	vertical-align: top;
}
#edit_supplier_info .box label {
/* 	margin-left: 20px; */
	width:100px;
}

</style>
<div class="modal fade" id="edit_supplier_info" tabindex="-1" role="dialog"
	aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document" style="width: 840px;">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">编辑供应商</h4>
			</div>
			<form id="edit_supplier_form" class="form">
			<input type="hidden" name="supplierid" id="supplierid">
			<div class="box formBox">
				<p>
					<label>代码</label>
					<input type="text" class="form-control" name="suppliercode" id="suppliercode" readonly placeholder="自动生成"></input>
					<label class="must">中文名称</label>
					<input type="text" class="form-control" name="chinesename" id="chinesename"></input>
				</p>
				<p>
					<label class="must">简称</label>
					<input type="text" class="form-control" name="supplierabbreviation" id="supplierabbreviation"></input>
					<label>英文名称</label>
					<input type="text" class="form-control" name="englishname" id="englishname"></input>
				</p>
			</div>
			<div class="box formBox">
				<div>
					<p>
						<label class="must">状态</label>
						<select name="status" id="status" class="form-control select">
							<option value="可用">可用</option>
							<option value="不可用">不可用</option>
						</select>
						<label class="must">E-MAIL</label>
						<input type="text" class="form-control" name="email" id="email"></input>
						<label class="must">公司电话</label>
						<input type="text" class="form-control" name="companytelephone" id="companytelephone"></input>
					</p>
					<p>
						<label class="must">传真</label>
						<input type="text" class="form-control" name="fax" id="fax"></input>
						<label class="must">联系人</label>
						<input type="text" class="form-control" name="contacts" id="contacts"></input>
						<label class="must">联系人电话</label>
						<input type="text" class="form-control" name="contactstelephone" id="contactstelephone"></input>
					</p>
					<p>
						<label class="must">开户银行</label>
						<input type="text" class="form-control" name="bankaccount" id="bankaccount"></input>
						<label class="must">银行账号</label>
						<input type="text" class="form-control" name="bankcode" id="bankcode"></input>
						<label class="must">开户人</label>
						<input type="text" class="form-control" name="accountholder" id="accountholder"></input>
					</p>
					<p>
						<label class="must">登录账号</label>
						<input type="text" class="form-control" name="loginaccount" id="loginaccount"></input>
						<label class="must">登录密码</label>
						<input type="password" class="form-control" name="loginpassword" id="loginpassword"></input>
						<label style="width:80px;">
							<input type="checkbox" id="password_flag" onclick="showOrHideEditPassword();"></input>显示密码
						</label>
					</p>
					<p>
						<label class="must">营业执照</label>
						<input type="text" class="form-control" name="businesslicense" id="businesslicense"></input>
						<label>组织机构代码证</label>
						<input type="text" class="form-control" name="organizationcodecertificate" id="organizationcodecertificate"></input>
						<label class="must">邮编</label>
						<input type="text" class="form-control" name="postalcode" id="postalcode"></input>
					</p>
					<p>
						<label class="must">法人代表</label>
						<input type="text" class="form-control" name="legalrepresentative" id="legalrepresentative"></input>
						<label class="must">税务登记号</label>
						<input type="text" class="form-control" name="taxregistrationnumber" id="taxregistrationnumber"></input>
						<a onclick="showSupplierAnnex($('#edit_supplier_info #supplierid').val());">
							附件
						</a>
					</p>
					<p>
						<label class="must">地区</label>
						<select name="nationalityid" id="nationalityid" class="form-control select" onchange="loadProvince(this,'#edit_supplier_info #provinceid');">
							<option value="-1">请选择</option>
							<c:forEach items="${nationalities }" var="nationality">
								<option value="${nationality.provincecityid }">${nationality.provincecityname }</option>
							</c:forEach>
						</select>
						<select name="provinceid" id="provinceid" class="form-control select" onchange="loadCity(this,'#edit_supplier_info #cityid');">
							<option value="">请选择</option>
						</select>
						<select name="cityid" id="cityid" class="form-control select">
							<option value="">请选择</option>
						</select>
					</p>
					<p>
						<label class="must">地址</label>
						<input type="text" class="form-control" name="address" id="address"></input>
					</p>
					<p>
						<label>说明</label>
						<textarea name="description" id="description"></textarea>
					</p>
				</div>
			</div>
			<div class="box updateBox">
				<p>
					<label>创建时间</label>
					<input type="text" class="form-control" readonly id="createtime"></input>
					<label>最后处理时间</label>
					<input type="text" class="form-control" readonly id="finalprocessingtime"></input>
				</p>
				<p>
					<label>创建人</label>
					<input type="text" class="form-control" readonly id="createpersonname"></input>
					<label>最后处理人</label>
					<input type="text" class="form-control" readonly id="lastpersonname"></input>
				</p>
			</div>
			</form>
			<div class="red" id="edit_supplier_msg" style="text-align:center;"></div>
			<div class="modal-footer">
				<c:if test="${isEdit }">
					<button type="button" class="btn btn1" onclick="saveEditSupplier();">保存</button>
				</c:if>
				<button type="button" class="btn btn2" data-dismiss="modal">取消</button>
			</div>
		</div>
	</div>
</div>
<script>

function editSupplier(id){
	$('#edit_supplier_info input:text').val("");
	$("#edit_supplier_info select").val("");
	$('#edit_supplier_info input:password').val("");
	$('#edit_supplier_info input:hidden').val("");
	$("#edit_supplier_info #password_flag")[0].checked=false;
	showOrHideEditPassword(false)
	$.ajax({
		url:"<%=basePath %>supplier/findSupplierById.do",
		type:"post",
		async : false,
		dataType:"json",
		data:"supplierid="+id,
		success:function(data){
			for(var key in data){
				$('#edit_supplier_info #'+key).val(data[key]);
			}
			var date=new Date();
			date.setTime(data.createtime);
			$("#edit_supplier_info #createtime").val(date.format('yyyy-MM-dd HH:mm:ss'));
			date.setTime(data.finalprocessingtime);
			$("#edit_supplier_info #finalprocessingtime").val(date.format('yyyy-MM-dd HH:mm:ss'));
			loadProvince('#edit_supplier_info #nationalityid','#edit_supplier_info #provinceid',data.provinceid);
			loadCity('#edit_supplier_info #provinceid','#edit_supplier_info #cityid',data.cityid);
		}
	});
	$('#edit_supplier_info').modal('show');
	
}

function saveEditSupplier(){
	if(!validateEditForm()) return;
	$.ajax({
		url:"<%=basePath %>supplier/update.do",
		type:"post",
		async : false,
		dataType:"json",
		data : $("#edit_supplier_form").serialize(),
		success:function(data){
			if(data.result=="success"){
				$("#edit_supplier_msg").html("");
				$('#edit_supplier_info :text').val("");
				$("#edit_supplier_info select").val("");
				$('#edit_supplier_info').modal('hide');
				location.href=location.href;
			}else{
				$("#edit_supplier_msg").html("编辑供应商失败：",data.error);
			}
		},
		error :function(){
			$("#edit_supplier_msg").html("编辑供应商失败！")
		}
	});
}

function validateEditForm(){
 	if(!validateString("中文名称","#edit_supplier_info [name='chinesename']",30,false)){
		return false;
	}
 	if(!validateString("供应商简称","#edit_supplier_info [name='supplierabbreviation']",10,true)){
		return false;
	}
	if(!validateString("英文名称","#edit_supplier_info [name='englishname']",30,true)){
		return false;
	}
	if(!validateSelect("状态","#edit_supplier_info [name='status']",false)){
		return false;
	}
 	if(!validateRegex("E-MAIL","#edit_supplier_info [name='email']",/^[a-zA-Z0-9][\w\._-]+?@([\w\-]+\.){1,2}[a-zA-Z]{2,3}$/,false)){
		return false;
	}
 	if(!validateRegex("公司电话","#edit_supplier_info [name='companytelephone']",/^[0-9]{3,4}-[0-9]{7,8}$/,false)){
		return false;
	}
 	if(!validateRegex("传真","#edit_supplier_info [name='fax']",/^[0-9]{3,4}-[0-9]{7,8}$/,false)){
		return false;
	}
	if(!validateString("联系人","#edit_supplier_info [name='contacts']",30,false)){
		return false;
	}
 	if(!validateRegex("联系人电话","#edit_supplier_info [name='contactstelephone']",/^1[0-9]{10}$/,false)){
		return false;
	}
	if(!validateString("开户银行","#edit_supplier_info [name='bankaccount']",60,false)){
		return false;
	}
 	if(!validateRegex("银行账号","#edit_supplier_info [name='bankcode']",/^[0-9]{16,19}$/,false)){
		return false;
	}
	if(!validateString("开户人","#edit_supplier_info [name='accountholder']",30,false)){
		return false;
	}
 	if(!validateRegex("登录账号","#edit_supplier_info [name='loginaccount']",/^[a-zA-Z0-9_]{6,30}$/,false)){
		return false;
	}
 	if(!validateRegex("登录密码","#edit_supplier_info [name='loginpassword']",/^[a-zA-Z0-9_]{6,30}$/,false)){
		return false;
	}
	if(!validateString("营业执照","#edit_supplier_info [name='businesslicense']",30,false)){
		return false;
	}
	if(!validateString("组织机构代码证","#edit_supplier_info [name='organizationcodecertificate']",30,false)){
		return false;
	}
 	if(!validateRegex("邮编","#edit_supplier_info [name='postalcode']",/^[0-9]{6}$/,false)){
		return false;
	}
	if(!validateString("法人代表","#edit_supplier_info [name='legalrepresentative']",30,false)){
		return false;
	}
	if(!validateString("税务登记号","#edit_supplier_info [name='taxregistrationnumber']",12,false)){
		return false;
	}
	if(!validateSelect("地区--国家","#edit_supplier_info [name='nationalityid']",false)){
		return false;
	}
	if(!validateSelect("地区--省份","#edit_supplier_info [name='provinceid']",false)){
		return false;
	}
	if(!validateSelect("地区--城市","#edit_supplier_info [name='cityid']",false)){
		return false;
	}
	if(!validateString("地址","#edit_supplier_info [name='address']",120,false)){
		return false;
	}
	if(!validateString("说明","#edit_supplier_info [name='description']",500,false)){
		return false;
	}
	return true;
}

function showOrHideEditPassword(flag){
	if(!flag){
		flag=$("#edit_supplier_info #password_flag")[0].checked;
	}
	if(flag){
		$("#edit_supplier_info #loginpassword").attr("type","text")
	}else{
		$("#edit_supplier_info #loginpassword").attr("type","password")
	}
}
</script>